<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2014/10/10
 * Time: 11:46
 * 后台管理人员
 */
class ManageUser extends CActiveRecord
{
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

    public function tableName()
    {
        return 'manage_user';
    }
    /**
     * 根据用户ID获取用户信息
     */
    public function findById($user_id){
        $info = $this->findByPk($user_id)->attributes;
        $last_info = explode('|', $info['lastlogin']);
        $info['last_ip'] = $last_info[0];
        $info['last_time'] = $last_info[1];
        return $info;
    }

    /**
     * 用户登陆
     */
    public function login($username, $password){
        $userinfo  = $this->find('username=:username and pwd=:pwd and status=:status', array(':username' => $username, ':pwd' => md5($password),':status' => 1));
        if ($userinfo) {
            //记录登陆时间和IP
            $userinfo->lastlogin = $userinfo->login_ip.'|'.$userinfo->login_time;
            $userinfo->login_time = date('Y-m-d H:i:s');
            $userinfo->login_ip = get_client_ip();
            $userinfo->loginnum += 1;
            $userinfo->save();
        }
        return $userinfo;
    }
}